/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package animation;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;

/**
 *
 * @author AJF
 */
/*--------------------------------------------------
 * AnimationCanvas.java
 *-------------------------------------------------*/
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;

public class AnimationCanvas extends GameCanvas implements Runnable {
    // Size of one frame in the spiral image

    private static final int FRAME_WIDTH = 57;
    private static final int FRAME_HEIGHT = 53;
    private AnimationSprite spSpiral; // Animated sprite
    private LayerManager lmgr;        // Manage layers
    private boolean running = false;  // Thread running?
    private final String BIRD_IMAGE = "/spiral.jpg";

    public AnimationCanvas() {
        // Gamecanvas constructor
        super(true);

        try {
            // Animated sprite
            //spSpiral = new AnimationSprite(Image.createImage("/spiral.jpg"),
            //        FRAME_WIDTH, FRAME_HEIGHT);
            //bird = new Sprite(loadImage(BIRD_IMAGE), 33, 31);

            spSpiral = new AnimationSprite(loadImage(BIRD_IMAGE), 
                    FRAME_HEIGHT, FRAME_WIDTH);
            
            // Change the reference pixel to the middle of sprite
            spSpiral.defineReferencePixel(FRAME_WIDTH / 2, FRAME_HEIGHT / 2);

            // Center the sprite on the canvas
            // (center of sprite is now in center of display)
            spSpiral.setRefPixelPosition(getWidth() / 2, getHeight() / 2);

            // Layer manager
            lmgr = new LayerManager();
            lmgr.append(spSpiral);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Image loadImage (String path){
        Image image=null;
        try{
            image = Image.createImage(path);

        }catch(Exception e){
        }
        return image;
    }
    
    /*--------------------------------------------------
     * Start thread
     *-------------------------------------------------*/
    public void start() {
        running = true;
        Thread t = new Thread(this);
        t.start();
    }

    /*--------------------------------------------------
     * Main loop
     *-------------------------------------------------*/
    public void run() {
        Graphics g = getGraphics();

        while (running) {
            // Update display
            drawDisplay(g);

            try {
                Thread.sleep(150);
            } catch (InterruptedException ie) {
                System.out.println("Thread exception");
            }
        }
    }

    /*--------------------------------------------------
     * Update display,
     *-------------------------------------------------*/
    private void drawDisplay(Graphics g) {
        // Animated sprite, show next frame in sequence
        spSpiral.nextFrame();

        // Paint layers
        lmgr.paint(g, 0, 0);

        // Flush off-screen buffer to display
        flushGraphics();
    }

    /*--------------------------------------------------
     * Stop thread
     *-------------------------------------------------*/
    public void stop() {
        running = false;
    }
}
